package com.tedu.jdbc.controller;

import com.tedu.jdbc.util.DBUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * @author blue sky
 * @version 1.0
 * @description TODO
 * @date 2023/4/29 10:14
 */

@Slf4j
@Controller
public class ArticleController {

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        log.info("开始处理文章发表的功能");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        //判断当前文章信息是否合法(不为空)
        if (title == null || title.isEmpty() ||
                author == null || author.isEmpty() ||
                content == null || content.isEmpty()
        ) {
            try {
                log.error("当前文章信息为空");
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (Connection connection = DBUtil.getConnection()){
            //检查标题是否重复
            String querySQL = "SELECT id FROM article WHERE title = ?";
            PreparedStatement queryPS = connection.prepareStatement(querySQL);
            queryPS.setString(1, title);
            ResultSet resultSet = queryPS.executeQuery();

            if (resultSet.next()) {
                log.error("已有相同标题,请换个名儿");
                response.sendRedirect("/article_fail.html");
                return;
            }

            //执行插入sql
            String insertSQL = "INSERT INTO article (title, author, content) " +
                               "VALUES (?, ?, ?)";
            PreparedStatement insertPS = connection.prepareStatement(insertSQL);
            insertPS.setString(1, title);
            insertPS.setString(2, author);
            insertPS.setString(3, content);
            int row = insertPS.executeUpdate();
            if (row > 0) {
                log.info("发表成功!");
                response.sendRedirect("/article_success.html");
            }

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request, HttpServletResponse response) {
        log.info("开始处理文章列表功能");

        response.setContentType("text/html;charset=utf-8");
        try (Connection connection = DBUtil.getConnection()){
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>ID</td>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("<td>正文</td>");
            pw.println("<td>操作</td>");
            pw.println("</tr>");

            Statement statement = connection.createStatement();
            String sql = "SELECT id, title, author, content FROM article";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                pw.println("<tr>");
                pw.println("<td>" + id + "</td>");
                pw.println("<td>" + resultSet.getString("title") + "</td>");
                pw.println("<td>" + resultSet.getString("author") + "</td>");
                pw.println("<td>" + resultSet.getString("content") + "</td>");
                pw.println("<td><a href = \"/deleteArticle?id=" + id + "\">删除</a></td>");
                pw.println("</tr>");
            }


            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/deleteArticle")
    public void deleteArticle(HttpServletRequest request, HttpServletResponse response) {
        log.info("开始处理删除文章功能");
        String id = request.getParameter("id");

        try (Connection connection = DBUtil.getConnection()) {
            String deleteSQL = "DELETE FROM article WHERE id = ?";
            PreparedStatement deletePS = connection.prepareStatement(deleteSQL);
            deletePS.setInt(1, Integer.parseInt(id));
            int row = deletePS.executeUpdate();
            if (row > 0) {
                log.info("删除成功");
                response.sendRedirect("/articleList");
            }

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}



















